﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SAPbouiCOM;
using System.Text.RegularExpressions;
using BoAddonBiz_FreightForwarder.BusinessBase;

namespace BoAddonBiz_FreightForwarder
{
    [ControlB1Form(IsNew = true, FormType = B1FormTypes.ado_AVA_FF_AirOrder)]
    class B1Form_AirOrder : SAPBoAddon.B1Form
    {
        protected override bool BeforeSaveCheckFormData()
        {
            return true;
        }

        protected override void FormEditModeChange(SAPBoAddon.frmItemEditMode frmEditMode)
        {

        }

        protected override void FormInfoSetting(SAPBoAddon.B1FormSetting FormSetting)
        {
            FormSetting.FormType = "AVA_FF_AirOrder";
            FormSetting.FormFileName = "AVA_FF_AirOrderForm.srf";

            #region 控件是否可编辑
            FormSetting.FormModeSetting.itmNewModeDisable = new string[] { "3", "7", "11", "39", "txt_1" };

            FormSetting.FormModeSetting.StatusItemUID = "7";
            SAPBoAddon.BrowseModeSetting ModeSetting = FormSetting.FormModeSetting.BrowseModeSettingList.Add();
            ModeSetting.Status = "P";
            ModeSetting.itmBrowseModeDisable = new string[] { "3", "5", "9", "11", "39", "txt_1" };

            ModeSetting = FormSetting.FormModeSetting.BrowseModeSettingList.Add();
            ModeSetting.Status = "R";
            ModeSetting.itmBrowseModeDisable = new string[] { "3", "5", "9", "11", "17", "39", "txt_1" };

            ModeSetting = FormSetting.FormModeSetting.BrowseModeSettingList.Add();
            ModeSetting.Status = "C";
            ModeSetting.itmBrowseModeDisable = Utils.GetFormItems(FormSetting.FormXmlDocument);

            FormSetting.FormModeSetting.itmFindModeEnable = new string[] { "3", "5", "7", "9", "11", "17", "39", "txt_1" };
            #endregion
        }

        protected override void InitializeForm(SAPBoAddon.B1FormInitializePar InitPar)
        {

        }
        public B1Form_AirOrder()
        {
            this.frm_ChooseFormListEvent += new SAPBoAddon.IB1FormEvents.B1ChooseFormListEventEventHandler(B1Form_AirOrder_frm_ChooseFormListEvent);
            this.frm_ItemEvent += new SAPBoAddon.IB1FormEvents.B1ItemEventEventHandler(B1Form_AirOrder_frm_ItemEvent);
            this.frm_MenuEvent += new SAPBoAddon.IB1FormEvents.B1MenuEventEventHandler(B1Form_AirOrder_frm_MenuEvent);
        }

        void B1Form_AirOrder_frm_MenuEvent(SAPBoAddon.B1AddonBase.B1MenuEvent pVal, ref bool BubbleEvent)
        {
            if (pVal.BeforeAction)
            {

            }
            else
            {
                if (pVal.MenuUID == B1MenuTypes.ado_SYS_Add)
                {
                    SAPBoAddon.B1Assistant.Item.SetValue(CurrentForm, "39", DateTime.Now.ToString("yyyyMMdd").ToString());
                    SAPBoAddon.B1Assistant.Item.SetValue(CurrentForm, "11", Utils.GetCurrentUser().Operator);
                }
            }
        }

        void B1Form_AirOrder_frm_ItemEvent(SAPBoAddon.B1AddonBase.B1ItemEvent pVal, ref bool BubbleEvent)
        {
            if (pVal.BeforeAction)
            {
                if (pVal.ItemUID == "1" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    SaveBeforeCheck(pVal.FormUID, ref BubbleEvent);
                }
                if (pVal.ItemUID == "5" && pVal.ItemChanged)
                {
                    this.ConfirmAirLineNum(pVal.ItemUID, ref BubbleEvent);
                }
                if (pVal.ItemUID == "9" && pVal.ItemChanged)
                {
                    this.MatchOrderNum(pVal.ItemUID, ref BubbleEvent);
                }
            }
            else
            {
                if (new string[] { "41" }.Contains<string>(pVal.ItemUID) && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    Utils.OpenUserDeFinedObjectForm(this.CurrentForm, pVal.ItemUID, "AVA_FF_Port");
                }
            }
        }

        void B1Form_AirOrder_frm_ChooseFormListEvent(SAPBoAddon.B1AddonBase.B1ChooseFormList pVal, ref bool BubbleEvent)
        {
            SAPbouiCOM.DBDataSource dbds = this.CurrentForm.DataSources.DBDataSources.Item("@AVA_FF_OAOD");
            if (pVal.Before_Action)
            {
            }
            else
            {
                if (pVal.Action_Success)
                {
                    /*航空公司*/
                    if (pVal.ChooseFromListUID == "CFL_AirLine")
                    {
                        dbds.SetValue("U_AirlineCode", dbds.Offset, pVal.SelectedObjects.GetValue("U_UseCode", 0).ToString());
                        dbds.SetValue("U_Airline", dbds.Offset, pVal.SelectedObjects.GetValue("U_AirLineName", 0).ToString());
                    }
                }
            }
        }

        private void SaveBeforeCheck(string FormUID, ref bool BubbleEvent)
        {
            SAPbouiCOM.Form oForm;
            SAPbouiCOM.DBDataSource dbds;
            SAPbobsCOM.Recordset ors;
            string OrderNumber = string.Empty;

            oForm = SBOApp.Forms.Item(FormUID);
            dbds = oForm.DataSources.DBDataSources.Item("@AVA_FF_OAOD");
            ors = SBOCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);
            if (oForm.Mode != BoFormMode.fm_ADD_MODE && oForm.Mode != BoFormMode.fm_UPDATE_MODE && oForm.Mode != BoFormMode.fm_EDIT_MODE) return;
            OrderNumber = SAPBoAddon.B1Assistant.Item.GetValue(oForm, "9").ToString();
            if (string.IsNullOrEmpty(OrderNumber))
            {
                BubbleEvent = false;
                SBOApp.MessageBox("主单号不能为空，请检查主单号！");
                return;
            }
            if (oForm.Mode == BoFormMode.fm_ADD_MODE)
            {
                string SQL_Check = string.Format("SELECT COUNT(1) FROM [@AVA_FF_OAOD] Where U_AirlineCode+N'-'+U_OrderNumber ='{0}' ", OrderNumber);
                ors.DoQuery(SQL_Check);
                if (ors.Fields.Item(0).Value > 0)
                {
                    BubbleEvent = false;
                    SBOApp.MessageBox("主单号：" + OrderNumber + " 已存在系统中，请检查主单号.");
                    return;
                }
            }

            ors.DoQuery("SELECT Autokey FROM ONNM  Where ObjectCode = 'AVA_FF_AirOrder'");
            dbds.SetValue("Code", dbds.Offset, ors.Fields.Item("Autokey").Value);
            dbds.SetValue("Name", dbds.Offset, ors.Fields.Item("Autokey").Value);

        }

        /// <summary>
        /// 验证后8位编码为数字
        /// </summary>
        /// <author>WangPeng</author>
        private void MatchOrderNum(string ItemUID, ref bool BubbleEvent)
        {
            string OrderNum = SAPBoAddon.B1Assistant.Item.GetValue(this.CurrentForm, ItemUID).ToString().Trim();
            //“主单号”：xxx – xxxxxxxx
            Regex regex_OrderNum = new Regex(@"\d{8}");
            if (!regex_OrderNum.IsMatch(OrderNum))
            {
                BubbleEvent = false;
                SBOApp.MessageBox("主单号不符合规则：123-12345678，请调整");
            }
        }
        /// <summary>
        /// 验证提单代码在航空公司列表中
        /// </summary>
        /// <author>WangPeng</author>
        private void ConfirmAirLineNum(string ItemUID, ref bool BubbleEvent)
        {
            string AirLineNum = SAPBoAddon.B1Assistant.Item.GetValue(this.CurrentForm, ItemUID).ToString().Trim();
            if (string.IsNullOrEmpty(AirLineNum))
            {
                return;
            }
            //“主单号”：xxx – xxxxxxxx
            Regex regex_OrderNum = new Regex(@"\d{3}");
            if (!regex_OrderNum.IsMatch(AirLineNum))
            {
                BubbleEvent = false;
                SBOApp.MessageBox("提单代码应该是数字，请调整.");
                return;
            }
            else
            {
                SAPbobsCOM.Recordset oRst = SBOCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);
                string SQL_CheckAirLineNum = string.Format("SELECT U_AirLineName FROM [@AVA_FF_OARL] WHERE U_UseCode=N'{0}'", AirLineNum);
                oRst.DoQuery(SQL_CheckAirLineNum);
                if (oRst.RecordCount <= 0)
                {
                    BubbleEvent = false;
                    SBOApp.MessageBox("提单代码不存在[航空公司]列表中，请调整.");
                    return;
                }
                else
                {
                    string AirLineName = oRst.Fields.Item("U_AirLineName").Value;
                    SAPbouiCOM.DBDataSource dbds = this.CurrentForm.DataSources.DBDataSources.Item("@AVA_FF_OAOD");
                    dbds.SetValue("U_Airline", dbds.Offset, AirLineName);
                }
            }
        }
    }
}
